//
//  ArtistCollectionViewCell.m
//  ArtDog
//
//  Created by dllo on 16/9/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ArtistCollectionViewCell.h"
#import "BaseTableView.h"
#import "GalleriesTableViewCell.h"
#import "MJRefresh.h"
#import "MJRefreshComponent+RefreshStyle.h"
#import "HLMacro.h"
#import "NSString+Common.h"
#import "ZWYNetTool.h"
#import "ArtistMainModel.h"

@interface ArtistCollectionViewCell ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet BaseTableView *tableView;

@property (nonatomic, assign) NSInteger pageIndex;

@property (nonatomic, strong) NSMutableArray<NSMutableArray *> *loadArray;

@end

@implementation ArtistCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    if (_loadArray == nil) {
        NSMutableArray *topicList = [NSMutableArray array];
        _loadArray = [NSMutableArray arrayWithObjects:topicList, topicList, topicList, nil];
    }
    [self setupBasic];
}

- (void)setupBasic
{
    
    [_tableView registerNib:[UINib nibWithNibName:@"GalleriesTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"galleriesTableViewCell"];
}

- (void)setType:(NSInteger)type
{
    
    _type = type;
    [_tableView reloadData];
    if (_tableView.mj_header == nil) {
        [self refreshTableView];
    }
    if (_loadArray[type].count == 0) {
        [self handleData];
    }
    
}

#pragma mark - 数据请求
- (void)handleData
{
    _pageIndex = 1;
    NSString *urlString = [self getUrlString];
    [ZWYNetTool GET:urlString andBody:nil andHeader:HOSTHEADER andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _loadArray[_type] = [NSMutableArray array];
        for (NSDictionary *dic in result[@"data"]) {
            ArtistMainModel *model = [ArtistMainModel modelWithDic:dic];
            [_loadArray[_type] addObject:model];
        }
        [_tableView.mj_header endRefreshing];
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        [_tableView.mj_header endRefreshing];
        NSLog(@"%@", error);
    }];
}

- (void)handleLoadData
{
    NSString *urlString = [self getUrlString];
    [ZWYNetTool GET:urlString andBody:nil andHeader:HOSTHEADER andResponse:ZWYJSON andSuccessBlock:^(id result) {
        for (NSDictionary *dic in result[@"data"]) {
            ArtistMainModel *model = [ArtistMainModel modelWithDic:dic];
            [_loadArray[_type] addObject:model];
        }
        [_tableView.mj_footer endRefreshing];
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        [_tableView.mj_footer endRefreshing];
        NSLog(@"%@", error);
    }];
}

- (NSString *)getUrlString
{
    NSString *urlString;
    
    urlString = [NSString getURLStringWith:HOST andPaths:@[@"artgoer/api/v1/user", [NSString getUserDefaultId], @"artist/v4/artistList"] andParams:@{@"pageIndex": [NSString stringWithFormat:@"%ld", _pageIndex], @"token": [NSString getUserDefaultToken], @"sortType": [NSString stringWithFormat:@"%ld", _type]}];
    
    return urlString;
}

#pragma mark - tableView 协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _loadArray[_type].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GalleriesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"galleriesTableViewCell"];
    cell.artistModel = _loadArray[_type][indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 + SCREEN_WIDTH / 3;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    CommunityMainCellContentController *cellContentVC = [[CommunityMainCellContentController alloc]init];
    //    cellContentVC.topicModel = _loadArray[_type][indexPath.row];
    //    UIView *view;
    //    while (1) {
    //        if ([view.nextResponder isKindOfClass:[UIViewController class]]) {
    //            break;
    //        } else {
    //            if (view == nil) {
    //                view = (UIView *)self.nextResponder;
    //            } else {
    //                view = (UIView *)view.nextResponder;
    //            }
    //        }
    //    }
    //    UIViewController *vc = (UIViewController *)view.nextResponder;
    //    [vc.navigationController pushViewController:cellContentVC animated:YES];
}


#pragma mark - 刷新
- (void)refreshTableView
{
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self handleData];
    }];
    [_tableView.mj_header mjRefreshStateHeaderStyle];
    
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (_loadArray[_type].count % 10 == 0) {
            _pageIndex = _loadArray[_type].count / 10 + 1;
        } else {
            _pageIndex = _loadArray[_type].count / 10 + 2;
        }
        [self handleLoadData];
    }];
    [_tableView.mj_footer mjRefreshAutoStateFooterStyle];
}



@end
